package com.axend.aerosense.home;

import android.os.Bundle;
import android.view.MenuItem;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.axend.aerosense.base.activity.MvvmBaseActivity;
import com.axend.aerosense.base.viewmodel.BaseViewModel;
import com.axend.aerosense.common.adapter.ScreenAutoAdapter;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.home.fragment.HomeAlarmListFragment;
import com.axend.aerosense.home.fragment.ReportLogListFragment;
import com.axend.aerosense.home.fragment.WardReportListFragment;
import com.axend.aerosense.home.R;
import com.axend.aerosense.home.databinding.HomeAlarmActivityMainBinding;

@Route(path = RouterActivityPath.Home.PAGE_HOME_WARNING)
public class ReportLogActivity extends MvvmBaseActivity<HomeAlarmActivityMainBinding, BaseViewModel>
        implements ITitle {
    private HomeAlarmListFragment homeAlarmFragment;
    private ReportLogListFragment reportLogFragment;
    private WardReportListFragment wardReportListFragment;
    enum OpenType{
        AlarmType(0),
        ReportType(1),
        WardReportType(2);

        int value;

        OpenType(int v){
            value = v;
        }

        public int getValue() {
            return value;
        }
    }
    @Autowired
    String roomUuid;  // 房间ID 如果为空 表示搜索所有, 不为空搜索指定房间
    @Autowired
    int openType;  // 打开方式: 0 表示报警, 1 表示搜索预警, 2 受监护人报表列表

    @Override
    public int getLayoutId() {
        return R.layout.home_alarm_activity_main;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected BaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ScreenAutoAdapter.match(this, 375.0f);
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this)
                .statusBarColor(android.R.color.white)
                .navigationBarColor(R.color.black)
                .autoDarkModeEnable(true)
                .fitsSystemWindows(true).init();
//        //刘海屏的刘海高度
//        int statusBarHeight = getStatusBarHeight();
//        viewDataBinding.homeActivityHome.setPadding(0,statusBarHeight,0,0);
        setSupportActionBar(viewDataBinding.homeToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);//左侧添加一个默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportFragmentManager().addOnBackStackChangedListener(getListener());
        init();
    }

    public void init(){
        if(OpenType.WardReportType.getValue() == openType){
            // 预警报告
            viewDataBinding.homeTitle.setText(R.string.home_ward_report);
            wardReportListFragment = WardReportListFragment.getInstance();
            changeFragment(wardReportListFragment);

        }else if(OpenType.ReportType.getValue() == openType){
            // 预警报告
            viewDataBinding.homeTitle.setText(R.string.home_warning_title);
            reportLogFragment = ReportLogListFragment.getInstance(roomUuid);
            changeFragment(reportLogFragment);


        }else {
            // 警报报告
            viewDataBinding.homeTitle.setText(R.string.home_alarmText);
            homeAlarmFragment = HomeAlarmListFragment.getInstance(roomUuid);
            changeFragment(homeAlarmFragment);
        }

    }

    public int getContentId(){
        return R.id.home_alarm_fragment;
    }

    private void changeFragment(Fragment fragment){
        FragmentUtil.add(this, getContentId(), fragment, null, true);
    }
    @Override
    public void setTitle(String title){
        viewDataBinding.homeTitle.setText(title);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() == 1){
            finish();
            return;
        }
        super.onBackPressed();
    }

    private FragmentManager.OnBackStackChangedListener getListener() {
        FragmentManager.OnBackStackChangedListener result = new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                FragmentManager manager = getSupportFragmentManager();
                if (manager != null) {
                    Fragment currFrag = (Fragment) manager.findFragmentById(getContentId());
                    currFrag.onResume();
                }
            }
        };

        return result;
    }
}
